home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 494 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  75 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c,gnu.gcc.help
  4. Subject: Re: Casting unsigned short as unsigned int -> Bus error
  5. Date: 5 Jan 1996 21:19:26 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4ck4ku$i5s@news.iag.net>
  8. References: <simmons.820857453@rzdspc1>
  9. NNTP-Posting-Host: pm3-orl15.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <simmons.820857453@rzdspc1>, 
  13. simmons@rzdspc1.informatik.uni-hamburg.de says...
  14. >
  15. >I have a pointer to a struct with a member declared as unsigned short:
  16. >
  17. >        typedef struct {
  18. >                ...
  19. >                unsigned short myshort;
  20. >                ...
  21. >                } RecType;
  22. >
  23. >        RecType *MyRec;
  24. >
  25. >Now when I try to dereference that member, casting it to (unsigned int),
  26. >I get a bus error:
  27. >
  28. >        unsigned int myint;
  29. >
  30. >        myint = (unsigned int) MyRec->myshort;  /* Bus error! */
  31. >
  32. >I'm using GCC 2.7.2 on a SparcStation running SunOS 4.1.4. I've made sure
  33. >that the pointer points to valid data.
  34.  
  35. I wish you had posted the code you used for this, because it certainly looks
  36. like you are trying to dereference an invalid or NULL pointer.
  37.  
  38. Try this snippet.  See if it gives you any problem.
  39.  
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43.  
  44. typedef struct 
  45.    {
  46.    unsigned short myshort;
  47.    } RecType;
  48.  
  49. int main( void)
  50.    {
  51.    RecType MyRec, *MyRecPtr;
  52.    unsigned int myint;
  53.  
  54.    MyRecPtr = malloc( sizeof(RecType)); 
  55.    if( MyRecPtr == NULL)
  56.       {
  57.       fprintf( stderr, "You've got a real memory problem here.\n");
  58.       exit(EXIT_FAILURE);
  59.       }
  60.  
  61.    MyRec.myshort = 4;
  62.    MyRecPtr->myshort = 6;
  63.    myint = MyRec.myshort + MyRecPtr->myshort;
  64.  
  65.    printf( "%u %u %u\n", (unsigned int)MyRec.myshort,
  66.                          (unsigned int)MyRecPtr->myshort,
  67.                          myint);
  68.    return 0;
  69.    }
  70.  
  71. -- 
  72. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  73. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  74.  
  75.